//
//  DYWVisitorView.swift
//  sinaWeibo
//
//  Created by 董艳武 on 16/2/5.
//  Copyright © 2016年 董艳武. All rights reserved.
//

import UIKit
// 代理 先声明 -- 必须继承自NSObjectProtocol，这样可以使用 weak 修饰
@objc protocol visitorViewDelegate:NSObjectProtocol {
    // 回调方法  
    // 登陆回调方法
    func didClickRegistButton()
    
    // 注册回调方法
     optional func didClickLoginButton ()
}
class DYWVisitorView: UIView {
    
    weak var delegate:visitorViewDelegate?

    // 重写父类的构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUI() {
        // 修改背景颜色
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
        // 添加子空间
        addSubview(imgView)
        // 添加约束
        // 关闭系统的 autoSizing
        imgView.translatesAutoresizingMaskIntoConstraints = false
        /**
        *  NSLayoutConstraint(
        item: 要给哪个控件设置约束,
        attribute: 设置对齐的方向,
        relatedBy: 大于等于，等于，小于等于,
        toItem: 参照的视图,
        attribute: ,
        multiplier: 系数（0~1）,
        constant: 微调值
        )
        */
        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterX, relatedBy:NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: imgView, attribute: .CenterY, relatedBy:NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 添加遮盖视图 并设置约束
        addSubview(coverImgView)
        coverImgView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverImgView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: 0))
        // 添加首页的小房子
        addSubview(houseImgView)
        houseImgView.translatesAutoresizingMaskIntoConstraints  = false
         addConstraint(NSLayoutConstraint(item: houseImgView, attribute: .CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
         addConstraint(NSLayoutConstraint(item: houseImgView, attribute: .CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 添加注册和登陆按钮
        addSubview(messageLabel)
        addSubview(registBtn)
        addSubview(loginBtn)
        // 提示 label 的约束 居中  距离顶部   宽度224
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        // 注册按钮的约束 宽 100 高 30 左边对齐 messageLabel 顶部和 messageLabel对齐
        registBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        // 登陆按钮的约束 宽 100 高 30 左边对齐 messageLabel 顶部和 messageLabel对齐
         loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 30))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 10))
        
     
        
    }
    
    func setupUIInfo(message:String , imgName:String?){
        
        if let OimgName = imgName {
            
            imgView.hidden = true
            
            messageLabel.text = message
            
            houseImgView.image = UIImage(named: OimgName)
        } else {
            imgView.hidden = false
        }
        
        
        
    }
    
    // 懒加载动画
    lazy var imgView:UIImageView = {
    
        let img:UIImage = UIImage(named: "visitordiscover_feed_image_smallicon")!
        let imgV:UIImageView = UIImageView(image:img )
        imgV.sizeToFit()
        return imgV
    }()

    // 懒加载中间的图片
    lazy var houseImgView:UIImageView = {
        let imgV:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
        imgV.sizeToFit()
        return imgV
    }()
    /// 懒加载提示语
    lazy var messageLabel:UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜"
        // 设置约束
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = .Center
        label.numberOfLines = 0
        return label
    }()
    /// 注册按钮    
    lazy var registBtn:UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("注册", forState:  UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)

        btn.addTarget(self, action: "didClickToRegist", forControlEvents: .TouchUpInside)
        return btn
    }()
    /// 登陆按钮
    lazy var loginBtn:UIButton = {
        
        let btn = UIButton(type: UIButtonType.Custom)
        let img = UIImage(named: "common_button_white_disable")
        btn.setBackgroundImage(img, forState: UIControlState.Normal)
//        img?.stretchableImageWithLeftCapWidth(10, topCapHeight: 10)
        btn.setTitle("登陆", forState:  UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.addTarget(self, action: "didClickToLogin", forControlEvents: .TouchUpInside)
        return btn
    }()
    /// 动画方法 
    func imgViewAnimating () {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * M_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 20
        // 当动画完成之后  或者 视图跑到后台之后 会默认移除 把 removedOnCompletion 改成 false 就不会移除了 
        animation.removedOnCompletion = false
        
        imgView.layer.addAnimation(animation, forKey: nil)
        
        
    }
    /// 遮盖视图
    private lazy var coverImgView:UIImageView = {
        
        let imgV:UIImageView = UIImageView()
        
        imgV.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        
        return imgV
        
    }()
    
    
    //MARK:--注册和登陆按钮的点击事件
    func didClickToRegist () {
        // 可选项相当于做了判断 
        // 强制解包 必须有值  
        
        delegate?.didClickRegistButton()
        
      
    }
    func didClickToLogin () {
        
        delegate?.didClickLoginButton?()
        
        
    }
}
